package thread;

import java.util.concurrent.ExecutionException;

import org.junit.Test;

public class ThreadTest {
	
	private static final int threadCount = 4;
	private static final int max = Integer.MAX_VALUE;
	private static Integer toMax = 0;

	@Test
	public void test() throws InterruptedException, ExecutionException{
		
		System.out.format("一共%s个线程开启\n", threadCount);
		for(int i = 0; i < threadCount; i++){
			Thread t = new Thread(){
				public void run() {
					System.out.println(Thread.currentThread().getId() + "：开始");
					for(; toMax < max; toMax++){}
					System.out.println(Thread.currentThread().getId() + "：结束");

					synchronized (ThreadTest.class) {
						if(toMax >= max){
							ThreadTest.class.notifyAll();
						}
					}
				}
			};
			t.start();
		}

		synchronized (ThreadTest.class) {
			ThreadTest.class.wait();
		}
		System.out.println("结束");
	}
}
